<?php
class ControllerModuleOceanicCustomFooter extends Controller
{
	private $error = array();

	public function index()
	{
    	$this->load->language('module/oceanic_custom_footer');
        $this->load->model('setting/setting');
		$this->document->setTitle($this->language->get('heading_title'));

		if(($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate())
		{
			$pos = 0;
			$request = $this->request->post;
			$settings = array();

			$settings['oceanic_custom_footer_module'][0]['layout_id'] = 0;
			$settings['oceanic_custom_footer_module'][0]['position'] = '';
			$settings['oceanic_custom_footer_module'][0]['sort_order'] = '';

			foreach($request as $key => $value)
			{
				$settings['oceanic_custom_footer_module'][0][$key] = $value;
			}

            $this->model_setting_setting->editSetting('oceanic_custom_footer', $settings);
			$this->session->data['success'] = $this->language->get('text_success');
			$this->redirect($this->url->link('extension/module', 'token=' . $this->session->data['token'], 'SSL'));
		}

        /* languge data */
		$this->data['heading_title'] = $this->language->get('heading_title');

		$this->data['text_enabled'] = $this->language->get('text_enabled');
		$this->data['text_disabled'] = $this->language->get('text_disabled');
		$this->data['text_contact_us'] = $this->language->get('text_contact_us');
		$this->data['text_about_us'] = $this->language->get('text_about_us');
        $this->data['text_news'] = $this->language->get('text_news');
		$this->data['text_twitter'] = $this->language->get('text_twitter');
		$this->data['text_social'] = $this->language->get('text_social');

		$this->data['entry_status'] = $this->language->get('entry_status');
		$this->data['entry_content'] = $this->language->get('entry_content');
		$this->data['entry_phone'] = $this->language->get('entry_phone');
		$this->data['entry_fax'] = $this->language->get('entry_fax');
		$this->data['entry_email'] = $this->language->get('entry_email');
		$this->data['entry_address'] = $this->language->get('entry_address');
		$this->data['entry_skype'] = $this->language->get('entry_skype');
		$this->data['entry_header'] = $this->language->get('entry_header');
		$this->data['entry_username'] = $this->language->get('entry_username');
		$this->data['entry_count'] = $this->language->get('entry_count');
		$this->data['entry_interval'] = $this->language->get('entry_interval');
		$this->data['entry_interval_help'] = $this->language->get('entry_interval_help');
		$this->data['entry_profile_id'] = $this->language->get('entry_profile_id');

		$language_data = array(
			'text_image_manager',
			// Entry News
			'entry_image',
			'entry_date',
			'entry_title',
			'entry_link',
			'entry_sort',
			// Text News
			'text_remove',
			'text_addnews',
			'text_browse',
			'text_clear',
		);

		for($i = 0; $i < sizeof($language_data); $i++)
		{        	$this->data[$language_data[$i]] = $this->language->get($language_data[$i]);
		}

		$this->load->model('tool/image');
		$this->data['img_no_image'] = $this->model_tool_image->resize('no_image.jpg', 48, 48);

		$this->data['button_save'] = $this->language->get('button_save');
		$this->data['button_cancel'] = $this->language->get('button_cancel');

        /* buttons */
		$this->data['action'] = $this->url->link('module/oceanic_custom_footer', 'token=' . $this->session->data['token'], 'SSL');
		$this->data['cancel'] = $this->url->link('extension/module', 'token=' . $this->session->data['token'], 'SSL');

		$this->data['token'] = $this->session->data['token'];

		$this->load->model('localisation/language');
		$this->data['languages'] = $this->model_localisation_language->getLanguages();

		$this->data['error_warning'] = (isset($this->error['warning'])) ? $this->error['warning'] : '';

		$this->data['breadcrumbs'] = array(
			array(
	       		'text'      => $this->language->get('text_home'),
				'href'      => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
	      		'separator' => false
	   		),
	   		array(
	       		'text'      => $this->language->get('text_module'),
				'href'      => $this->url->link('extension/module', 'token=' . $this->session->data['token'], 'SSL'),
	      		'separator' => ' :: '
	   		),
	   		array(
	       		'text'      => $this->language->get('heading_title'),
				'href'      => $this->url->link('module/oceanic_custom_footer', 'token=' . $this->session->data['token'], 'SSL'),
	      		'separator' => ' :: '
   			),
		);

		/* load settings */
		$def_config = array(
			'status'	=> 0,
			'rcf_contact_us'=> array(
				'status'	=> 0,
				'header'	=> array(),
				'phone'		=> array(),
				'fax'		=> array(),
				'email'		=> array(),
				'address'	=> array(),
				'skype'		=> array(),
			),
			'rcf_about_us' 	=> array(
            	'status'	=> 0,
            	'content'	=> array(),
			),
			'rcf_news' 	=> array(
            	'status'	=> 0,
            	'rows'		=> array(),
			),
			'rcf_twitter'	=> array(
				'status'	=> 0,
				'username'	=> '',
				'count'		=> 2,
				'interval'	=> 0,
			),
			'rcf_social'	=> array(
				'status'	=> 0,
				'facebook'	=> '',
				'twitter'	=> '',
				'rss'		=> '',
				'dribbble'	=> '',
				'forrst'	=> '',
				'vimeo'		=> '',
				'linkedin'	=> '',
			),
		);

		$config = $this->config->get('oceanic_custom_footer_module');
		$config = $config[0];

		foreach($def_config as $name => $value)
		{
			if(isset($this->request->post[$name]))
			{
				$this->data[$name] = $this->request->post[$name];
			}
			else if(isset($config[$name]))
			{
				$this->data[$name] = $config[$name];
			}
			else
			{
           		$this->data[$name] = $value;
			}
		}

		if(!isset($this->data['rcf_news']['rows']))
		{
        	$this->data['rcf_news']['rows'] = array();
		}

		foreach($this->data['rcf_news']['rows'] as &$news)
		{
			if($news['image'] != '')
			{
				$news['thumb'] = $this->model_tool_image->resize($news['image'], 48, 48);
			}
			else
			{
				$news['thumb'] = $this->data['img_no_image'];
			}
		}

		/* end */
		$this->template = 'module/oceanic_custom_footer.tpl';
		$this->children = array(
			'common/header',
			'common/footer'
		);
		$this->response->setOutput($this->render());
	}

	private function validate()
	{
		if(!$this->user->hasPermission('modify', 'module/welcome'))
		{
			$this->error['warning'] = $this->language->get('error_permission');
		}

		return (!$this->error) ? true : false;
	}
}
?>